﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
using System.Threading;
using System.Net;
using SD.Database;

namespace smallDeimos
{
    public interface ISchedulerJob
    {
        void Execute();
    }

    public class SampleJob : ISchedulerJob
    {
        public void Execute()
        {
           // StoredProcedures.InsertNews(8, DateTime.Now, "Scheduler tick");
            try
            {
                UpdateFleets();
                UpdatePlanets();
                UpdateSciences();
                UpdateTrade();
                WebRequest req = WebRequest.Create(new Uri(@"http://www.smalldeimos.qsh.eu/Game/News.aspx"));
                WebResponse resp = req.GetResponse();
            }
            catch (WebException ex)
            {
                return;
            }
            catch (Exception ex)
            {
                StoredProcedures.InsertNews(8, DateTime.Now, ex.ToString());
            }
        }
        private void UpdateTrade()
        {
            // throw new NotImplementedException();
        }

        private void UpdateSciences()
        {
            StoredProcedures.UpdateSciences();
            StoredProcedures.UpdateCulture();
        }

        private void UpdatePlanets()
        {
            StoredProcedures.UpdatePlanets();
        }

        private void UpdateFleets()
        {
            StoredProcedures.UpdateFleets();
        }
    }
    public class SchedulerConfiguration
    {
        private int sleepInterval;
        private ArrayList jobs = new ArrayList();

        public int SleepInterval { get { return sleepInterval; } }
        public ArrayList Jobs { get { return jobs; } }

        public SchedulerConfiguration(int newSleepInterval)
        {
            sleepInterval = newSleepInterval;
        }
    }
    public class Scheduler
    {
        private SchedulerConfiguration configuration = null;

        public Scheduler(SchedulerConfiguration config)
        {
            configuration = config;
        }

        public void Start()
        {
            while (true)
            {
                try
                {
                    //for each job, call the execute method
                    foreach (ISchedulerJob job in configuration.Jobs)
                    {
                        job.Execute();
                    }
                }
                catch (Exception) { }
                finally
                {
                    Thread.Sleep(configuration.SleepInterval);
                }
            }
        }
    }

}
